package stack;

import linearList.SeqList;

/**
 * 顺序表实现栈操作，入栈、出栈采用尾插入、尾删除
 * @param <T>
 */
public class SeqStack<T> implements Stack<T> {
    private SeqList<T> list;

    public SeqStack() {
        this(64);
    }

    public SeqStack(int length) {
        this.list = new SeqList<T>(length);
    }

    @Override
    public String toString() {
        return this.getClass().getName()+" "+this.list.toString();
    }

    @Override
    public boolean isEmpty() {
        return this.list.isEmpty();
    }

    @Override
    public void push(T x) {
        this.list.insert(x);
    }

    @Override
    public T peek() {
        return this.list.get(this.list.size() - 1 );
    }

    @Override
    public T pop() {
        return this.list.remove(this.list.size() - 1 );
    }

    public static void main(String args[]) {
        SeqStack<Character> stack1 = new SeqStack<Character>(20);
        System.out.print("SeqStack Push: ");
        char[] array = {'w', 'h', 'b', 'y', 't'};
        for (char ch : array) {
            stack1.push(ch);
            System.out.print(ch + "  ");
        }
        System.out.println();
        System.out.print("SeqStack Pop: ");
        while (!stack1.isEmpty()) {
            System.out.print(stack1.pop().toString() + "  ");
        }
        System.out.println();
    }
}
